package com.cx.mall.user.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cx.mall.user.entity.UserLoginLog;
import com.cx.mall.user.mapper.IUserLoginLogMapper;
import com.cx.mall.user.model.UserLoginLogQuery;
import com.cx.mall.user.service.IUserLoginLogService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service // 切记，将类放到 Spring 容器中
public class UserLoginLogServiceImpl extends ServiceImpl<IUserLoginLogMapper, UserLoginLog> implements IUserLoginLogService {
    @Override
    public List<UserLoginLog> list(UserLoginLogQuery query) {
        // 1 创建链式条件查询器，在 Mapper 层构建查询条件
        LambdaQueryChainWrapper<UserLoginLog> queryWrapper = new
                LambdaQueryChainWrapper<>(baseMapper);
        // 2 构建条件
        // 2.1 如果 userId 不为空，按 userId 查询
        if(query.getUserId() != null){
            queryWrapper.eq(UserLoginLog::getUserId,query.getUserId());
        }
        // 2.2 如果起始时间和终止时间不为空
        if(query.getStartTime() != null && query.getStopTime() != null){

            queryWrapper.between(UserLoginLog::getCreateTime,query.getStartTime(),query.getStopTime()
            );
        }else if(query.getStartTime() != null) { // 如果起始时间不为空
            queryWrapper.ge(UserLoginLog::getCreateTime,query.getStartTime());
        }else if(query.getStopTime() != null){ // 如果终止时间不为空
            queryWrapper.le(UserLoginLog::getCreateTime,query.getStopTime());
        }
        return queryWrapper.list();
    }
    @Override
    public IPage<UserLoginLog> page(UserLoginLogQuery query, Page page) {
        LambdaQueryChainWrapper<UserLoginLog> queryWrapper = new
                LambdaQueryChainWrapper<>(baseMapper);
        // 2 构建条件
        // 2.1 如果 userId 不为空，按 userId 查询
        if(query.getUserId() != null){
            queryWrapper.eq(UserLoginLog::getUserId,query.getUserId());
        }
        // 2.2 如果起始时间和终止时间不为空
        if(query.getStartTime() != null && query.getStopTime() != null){

            queryWrapper.between(UserLoginLog::getCreateTime,query.getStartTime(),query.getStopTime()
            );
        }else if(query.getStartTime() != null) { // 如果起始时间不为空
            queryWrapper.ge(UserLoginLog::getCreateTime,query.getStartTime());
        }else if(query.getStopTime() != null){ // 如果终止时间不为空
            queryWrapper.le(UserLoginLog::getCreateTime,query.getStopTime());
        }
        return queryWrapper.page(page);
    }
}

